package 第二章数据结构.并查集.id837连通块中点的数量;

import java.util.Scanner;

public class Main {
    //记录每个集合的父节点
    static int[] p;
    //记录每个集合中的点的数量
    static int[] cnt;
    public static void main(String[]args){
        Scanner scanner = new Scanner(System.in);
        int n = scanner.nextInt();
        int m = scanner.nextInt();
        scanner.nextLine();
        //初始化
        p = new int[n+1];
        cnt = new int[n+1];
        for(int i = 1;i<=n;i++){
            p[i] = i;
            cnt[i] = 1;
        }

        while (m -- >0){
            String s = scanner.nextLine();
            String[] op = s.split(" ");
            if(op[0].equals("C")){
                int a = Integer.parseInt(op[1]);
                int b = Integer.parseInt(op[2]);
                merge(a,b);
            }else if(op[0].equals("Q1")){
                int a = Integer.parseInt(op[1]);
                int b = Integer.parseInt(op[2]);
                if(query(a,b))System.out.println("Yes");
                else System.out.println("No");
            }else if(op[0].equals("Q2")){
                int a = Integer.parseInt(op[1]);
                System.out.println(cnt[find(a)]);
            }
        }
    }

    public static void merge(int a, int b){
        //防止重复
        if(find(a) == find(b))return;
        cnt[find(b)] += cnt[find(a)];
        p[find(a)] = find(b);
    }
    public static boolean query(int a, int b){
        return find(a) == find(b);
    }
    public static int find(int x){
        if (p[x] != x){
            p[x] = find(p[x]);
        }
        return p[x];
    }


}
